jQuery(function() {
	//获取选中状态
	$(function(){
		  $(":radio").click(function(){
			  serverData.key = jQuery("#keyword").val();
			  var isSucceed=$('input:radio[name="isReceived"]:checked').val();
			  serverData.isSucceed = isSucceed;
			  serverTable.draw();
		  });
	});
    //已接收信息列表
	var isSucceed =  $('input:radio[name="isReceived"]:checked').val();
    var serverData = {};
    serverData.key = '';
    serverData.isSucceed = isSucceed;
    var serverTable; 
    jQuery(document).ready(function () {
    	serverTable = jQuery('#chanceTranceTable').DataTable({
	        "serverSide": true,
	        "ordering": false,
	        "ajax": {
	            "dataType": "json",
	            "type": "get",
	            "cache": false,
	            "url":window.rootPath+ '/ui/chanceReceive/getMyChance',
	            "data": function (d) {
	                return $.extend({}, d, serverData);
	            }
	        },
	        "iDisplayLength": 5,
	        columns: [
	            {
	            data: null
	            },
	            {
	            	data: null,
	                defaultContent: "",
	                render: function (data, type, full, meta) {
	                	var operate;
	                		operate ='<input type="checkbox" class="checkchild">';
	                    
	                    return operate;

	                }
		         },
		         {
	            	 data: null,
					 render: function(data, type, full, meta){
						var dOut;
	                    var isSubmit=data.saleChance.isSubmit;
	                    var isSucceed = data.saleChance.isSucceed;
	                    if(isSucceed != null){
	                		dOut ='<p><span class="badge" style="background-color:red;font-family:arial;color:bule">已关闭-'+data.saleChance.isSucceed+'</span></p>';
	                	}else{
	                    	dOut = '<p><span class="badge">处理中</span></p>';
						}
	                    return dOut;
					 }
	            },
	            {
		        	 data: 'saleChance.name',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
							}
						}
	            },
	            {
	            	data: 'saleChance.chancePriority'
	            },
	            {
	                data: 'saleChance.source'
	            },
	            {
	                data: 'saleChance.sourceUnit',
					render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
						}
					}
	                
	            },
	            {
	                data: 'saleChance.user.empLogin.userName'
	                
	            },
	            {
	            	data: 'saleChance.business'
	            		
	            },
	            {
	            	data: 'saleChance.user.saleArea.superiorArea'
	            },
	            {
	                data: 'saleChance.sourceOfferInfo',
					render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
						}
					}
	                
	            },
	            {
	                data: 'saleChance.projectState',
					render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
						}
					}
	            },
	            {
	                data: 'saleChance.deviceState'
	            },
	            {
	                data: 'saleChance.sourcePower'
	            },
	            {
	                data: 'saleChance.orderAmount'
	                
	            },
	            {
	                data: 'saleChance.orderUnit'
	                
	            },
	            {
	                data: 'saleChance.orderPerson'
	                
	            },
	            {
	                data:  'saleChance.orderPersonPhone',
					render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
						}
					}
	                
	            },
	            {
	                data: 'saleChance.orderUnitAddr',
					render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
						}
					}
	                
	            },
	            {
	            	data: 'saleChance.createTime',
	            	render : function(data,type, full,meta) {
							if (data) 
							{
								return moment(data).format("YYYY-MM-DD");
							}
	            	}
	                
	            } 
	          ],
	        "drawCallback": function (oSettings) {
	            var k = 0;
	            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

	            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                k++;
	            }

	            serverTable.off('click', 'tr');
	            serverTable.on('click', 'tr', function (event) {
	                event = event || window.event;
	                var target = event.target || event.scrElement;
	                var data = serverTable.row(this).data();
	                serverData1.chanceId = data.saleChance.id;
					serverData2.chanceId = data.saleChance.id;
		    		serverTable1.draw();
		    		serverTable2.draw();
	                return;
	            });
	        }
	        
	    });

    	});
    jQuery('.trance-btn').on("click",function()
    		{
		       var selects=getSelect("#chanceTranceTable",1);
		   	   if(selects.length>1){
		   		   $.springLemonDialog({
		   		         content: '只能操作一条信息',// 确认文本
		   		         clickButton: function (sender, modal, index) {// 按钮事件处理
		   		             $(this).closeDialog(modal);
		   		         }
		   		   }); 
		   	   }
   	   else if(selects.length==0){
   		   $.springLemonDialog({
   		         content: '请选择要操作的信息',// 确认文本
   		         clickButton: function (sender, modal, index) {// 按钮事件处理
   		             $(this).closeDialog(modal);
   		         }
   		   }); 
   	   }else if(selects.length==1){
   		 var id=selects[0].id;
   		 var name =selects[0].saleChance.name;
   		 var chanceId =selects[0].saleChance.id;
   		if(selects[0].saleChance.closeReason != null){
   			$.springLemonDialog({
   		         content: '该信息已关闭！',// 确认文本
   		         clickButton: function (sender, modal, index) {// 按钮事件处理
   		             $(this).closeDialog(modal);
   		         }
   		   });
   		 }
   		else{
   			jQuery("#chanceName").val(name);
   			jQuery("#chanceId").val(chanceId);
   			jQuery("#tranceInfo").val("");
   			jQuery("#tranceId").val("");
   		    jQuery("#tranceResult").val("");
   		    jQuery("#orderAmount").val("");
   		    jQuery("#chanceTranceModal").modal('show'); 
   		}
   		
   	   }
    		});
  //查看详情
    jQuery('.detail-btn').on('click', function() {
    	var selects=getSelect("#chanceTranceTable",1);
  	   if(selects.length>1){
  		   $.springLemonDialog({
  		         content: '只能操作一条记录',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }
  	   else if(selects.length==0){
  		   $.springLemonDialog({
  		         content: '请选择要操作的记录',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }else if(selects.length==1){
  		 var id=selects[0].saleChance.id;
  		window.location = window.rootPath + "/ui/chance/detail"+"?chanceId="+id;  
  	   }
	});
       jQuery('.saveBtn').on("click",function()
    		{
    	        var tranceId = jQuery("#tranceId").val();
    	        var chanceId =jQuery("#chanceId").val();
    	        var userId =jQuery("#userId").val();
    	        var tranceInfo =jQuery("#tranceInfo").val();
    	        var orderAmount =jQuery("#orderAmount").val();
    	        var tranceResult =jQuery("#tranceResult").val();
    	        jQuery(".btn").attr("disabled", true);
    	        /*if(orderAmount == "" || orderAmount == null){
    	        	jQuery(".btn").attr("disabled", false);
    	        }else{*/
    	        //if(tranceId == ""){
    	        	var url = window.rootPath + "/ui/chanceTrance/save";
        	        var d={
        	        		"id": tranceId,
        	        		"orderAmount": orderAmount,
        					"user": {"id":userId},
        					"saleChance": {"id":chanceId},
        					"tranceInfo": tranceInfo,
        					"tranceResult": tranceResult
        			};
        			jQuery.ajax({
        				type : 'post',
        				url : url,
        				data : JSON.stringify(d),
        				dataType : 'json',
        				contentType : 'application/json;charset=utf-8',
        				success : function(data) {
        					jQuery(".btn").attr("disabled", false);
        					jQuery("#chanceTranceModal").modal('hide');
        					serverData1.chanceId = chanceId;
        					serverData2.chanceId = chanceId;
        		    		serverTable1.draw();
        		    		serverTable2.draw();
        				}
        			});	
    	        //}
    			
    		});
       //我的跟踪tab页数据
       var serverData1 = {};
       var serverTable1;
       jQuery(document).ready(function () {
       	serverTable1 = jQuery('#myTranceTable').DataTable({
   	        "serverSide": true,
   	        "ordering": false,
   	        "ajax": {
   	            "dataType": "json",
   	            "type": "get",
   	            "cache": false,
   	            "url":window.rootPath+ '/ui/chanceTrance/getMyTrance',
   	            "data": function (d) {
   	                return $.extend({}, d, serverData1);
   	            }
   	        },
   	        "iDisplayLength": 5,
   	        columns: [
   	            {
   	            	data: null,
   	                defaultContent: "",
   	                render: function (data, type, full, meta) {
   	                	var operate;
   	                		operate ='<input type="checkbox" class="checkTrance">';
   	                    return operate;

   	                }
   	            },
   	            {
   	            	
   	                   data: 'createTime',
            	       render : function(data,type, full,meta) {
						       if (data) 
						      {
							      return moment(data).format("YYYY-MM-DD");
						      }
            	}
   		         },
   	            {
   	                data: 'user.empLogin.userName'
   	            },
   	            {
   	            	data: null,
   	                defaultContent: "",
   	                render: function (data, type, full, meta) {
   	                	var text;
   	                		text ='<p>'+data.user.saleArea.superior+"-"+data.user.saleArea.areaName+'</p>';
   	                    
   	                    return text;

   	                }
   	            },
   	            {
   	                data: 'tranceInfo',
					render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 15) ? '<font title="'+data+'">'+data.substring(0,15)+'...'+'</font>' : data;
						}
					}
   	                
   	            },
   	            {
   	                data: 'tranceResult',
					render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 15) ? '<font title="'+data+'">'+data.substring(0,15)+'...'+'</font>' : data;
						}
					}
   	                
   	            },
   	            {
   	                data: 'orderAmount'
   	                
   	            }
   	          ],
   	        "drawCallback": function (oSettings) {
   	            var k = 0;
   	            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
   	         
   	            serverTable1.off('click', 'tr');
   	            serverTable1.on('click', 'tr', function (event) {
   	                event = event || window.event;
   	                var target = event.target || event.scrElement;
   	                var data = serverTable1.row(this).data();

   	                return;
   	            });
   	        }
   	        
   	    });

       	});
       jQuery('.edit-btn').on("click",function()
       		{
       		var selects=getSelect("#myTranceTable",0);
      	   if(selects.length>1){
      		   $.springLemonDialog({
      		         content: '只能操作一条信息',// 确认文本
      		         clickButton: function (sender, modal, index) {// 按钮事件处理
      		             $(this).closeDialog(modal);
      		         }
      		   }); 
      	   }
      	   else if(selects.length==0){
      		   $.springLemonDialog({
      		         content: '请选择要操作的信息',// 确认文本
      		         clickButton: function (sender, modal, index) {// 按钮事件处理
      		             $(this).closeDialog(modal);
      		         }
      		   }); 
      	   }else if(selects.length==1){
      		 var id=selects[0].id;
      		 var tranceInfo=selects[0].tranceInfo;
      		 var tranceResult=selects[0].tranceResult;
      		 var name =selects[0].saleChance.name;
      		 var chanceId =selects[0].saleChance.id;
      		 var orderAmount =selects[0].orderAmount;
      		jQuery("#orderAmount").val(orderAmount);
      		jQuery("#tranceId").val(id);
       		jQuery("#chanceName").val(name);
       		jQuery("#chanceId").val(chanceId);
       		jQuery("#tranceInfo").val(tranceInfo);
       	    jQuery("#tranceResult").val(tranceResult);
       	    jQuery("#chanceTranceModal").modal('show');
      	   }
       		});
       jQuery('.delete-btn').on("click",function(){
    	   var selects=getSelect("#myTranceTable",0);
       	   var url = window.rootPath + "/ui/chanceTrance/delete";
       	   delTranceSelect(selects,url,serverTable1) 
       });
       //所有跟踪tab页数据
       var serverData2= {};
       var serverTable2;
       jQuery(document).ready(function () {
       	serverTable2 = jQuery('#allTranceTable').DataTable({
   	        "serverSide": true,
   	        "ordering": false,
   	        "ajax": {
   	            "dataType": "json",
   	            "type": "get",
   	            "cache": false,
   	            "url":window.rootPath+ '/ui/chanceTrance/getAllTrance',
   	            "data": function (d) {
   	                return $.extend({}, d, serverData1);
   	            }
   	        },
   	        "iDisplayLength": 5,
   	        columns: [
   	            {
   	            	
   	                   data: 'createTime',
            	       render : function(data,type, full,meta) {
						       if (data) 
						      {
							      return moment(data).format("YYYY-MM-DD");
						      }
            	            }
   		         },
   	            {
   	                data: 'user.empLogin.userName'
   	            },
   	            {
   	            	data: null,
   	                defaultContent: "",
   	                render: function (data, type, full, meta) {
   	                	var text;
   	                		text ='<p>'+data.user.saleArea.superior+"-"+data.user.saleArea.areaName+'</p>';
   	                    
   	                    return text;

   	                }
   	            },
   	            {
   	                data: 'tranceInfo',
					render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 15) ? '<font title="'+data+'">'+data.substring(0,15)+'...'+'</font>' : data;
						}
					}
   	                
   	            },
   	            {
   	                data: 'tranceResult',
					render:function(data,type,full,meta){
						if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 15) ? '<font title="'+data+'">'+data.substring(0,15)+'...'+'</font>' : data;
						}
					}
   	                
   	            }
   	          ],
   	        "drawCallback": function (oSettings) {
   	            var k = 0;
   	            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
   	         
   	            serverTable2.off('click', 'tr');
   	            serverTable2.on('click', 'tr', function (event) {
   	                event = event || window.event;
   	                var target = event.target || event.scrElement;
   	                var data = serverTable1.row(this).data();
   	                
   	                return;
   	            });
   	        }
   	        
   	    });
       	
       	});
	   ///////列表中的全选
       $("#checkAll").click(function(){
       	var check = $(this).prop("checked");
           $(".checkchild").prop("checked", check);
   		});
	   ///////我的跟踪列表中的全选
       $("#checkAllTrance").click(function(){
       	var check = $(this).prop("checked");
           $(".checkTrance").prop("checked", check);
   		});
       
       $('#myTabs a:first').tab('show');
function delTranceSelect(selects,url,tableName){
    		if(selects.length==0){
    		       $.springLemonDialog({
    		         content: '请选择删除的记录',// 确认文本
    		         clickButton: function (sender, modal, index) {// 按钮事件处理
    		             $(this).closeDialog(modal);
    		         }
    		       }); 
    		   }else{
    			   var ids=selects[0].id;
    			   for(var i=1;i<selects.length;i++){
    				   ids += ',' + selects[i].id;  
    			   }
    			   var api=url+"?ids="+ids;
    			   $.springLemonDialog({
    			         content: '确认删除'+selects.length+'条记录？',// 确认文本
    			         clickButton: function (sender, modal, index) {// 按钮事件处理
    			        	 if (index == 0) {
    			        		 jQuery.ajax({
    			        			    url:encodeURI(api),
    			        	            cache:false,
    			        	            type :"get",
    			        	            dataType:"json",
    			        	            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
    			        	            success:function(data){
    			        	            	$.springLemonDialog({
    			        	       	         content: '记录已删除！！',// 确认文本
    			        	       	         clickButton: function (sender, modal, index) {// 按钮事件处理
    			        	       	             $(this).closeDialog(modal);
    			        	       	         }
    			        	       	       });
    			        	            	tableName.draw(false); 
    			        	            	serverTable2.draw(false);
    			        	            }
    			        	        });	
    			        	 }
    			             $(this).closeDialog(modal);
    			         }
    			       });  
    		   }
    		
    	}
//查询
jQuery('.searchBtn').on('click', function() {
	serverData.key = jQuery('#keyword').val();
	serverTable.draw();
});
// 撤销查询
jQuery('.cancelBtn').on("click", function() {
	jQuery('#keyword').val('');
	serverData.key = '';
	serverTable.draw();
});
});